package com.h4313.ghome;

import java.io.InputStream;
import java.security.KeyStore;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import android.app.Application;
import android.content.Context;

public class GHomeApplication extends Application {

	public static final boolean DEBUG_MODE = true;
	public static String SERVER_AUTH = "YWRtaW46YWRtaW4=";
	/**
     * Base URL of the Demo Server (such as http://my_host:8080/gcm-demo)
     */
    public static String SERVER_URL = "https://flolom.no-ip.org:2002";
//	public static final String SERVER_URL = "https://192.168.0.12:2002";

    /**
     * Google API project id registered to use GCM.
     */
    public static final String SENDER_ID = "702444405811"; // Rasp ID
    
    @Override
    public void onCreate() {
    	super.onCreate();
    	
    	sslFactory = newSslSocketFactory(this);
    }
    
    
    private static SSLSocketFactory sslFactory;
    private SSLSocketFactory newSslSocketFactory(Context context) {
		try {
			// On obtient une instance de notre KeyStore
			KeyStore trusted = KeyStore.getInstance("BKS");
			InputStream in = context.getResources().openRawResource(
					R.raw.ghome_keystore);
			try {
				// Initialisation de notre keystore. On entre le mot de
				// passe (storepass)
				trusted.load(in, "ghomeghome".toCharArray());
			} finally {
				in.close();
			}

			// Passons le keystore au SSLSocketFactory qui est responsable
			// de la verification du certificat
			/* Load the keyStore that includes self-signed cert as a "trusted" entry. */
			TrustManagerFactory tmf = 
			  TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
			tmf.init(trusted);
			SSLContext ctx = SSLContext.getInstance("TLS");
			TrustManager[] manager = tmf.getTrustManagers();
			
			ctx.init(null, tmf.getTrustManagers(), null);
			SSLSocketFactory factory = ctx.getSocketFactory();
			return factory;
			
		} catch (Exception e) {
			throw new AssertionError(e);
		}
	}
    
    public static SSLSocketFactory getSSLSocketFactory() {
    	return sslFactory;
    }
}
